package server.gui;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.*;
import java.io.IOException;
import javax.swing.JLabel;
import javax.swing.JList;
import database.*;
import server.EchoServer;
import javax.swing.JTextField;

/**
 * This class responsible Server's GUI
 * @author Jacob
 * @author Michael
 * @author Amir
 *
 */

public class Server_GUI extends JFrame {


	private static final long serialVersionUID = 1L;

	private JPanel FirstPanel = null;
	private EchoServer server=null;  

	private JButton closeBtn = null;
	private JButton connectBtn = null;

	private DefaultListModel clientConnectionModelList=null;

	private JLabel jLabel = null;
	private JLabel userLabel = null;
	private JLabel pwLabel = null;
	protected GUI_ImagePanel background;
	private Toolkit toolkit = Toolkit.getDefaultToolkit();
	private Dimension scrnsize = toolkit.getScreenSize();
	private JTextField userTextField;
	private JTextField pwTextField;
	public static String sqlUser = new String("root");
	public static String sqlPW = new String("root");
	public static JDBC jdbc;
	/**
	 * This is the default constructor
	 */
	public Server_GUI() {
		
		
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		
		background = new GUI_ImagePanel(new ImageIcon("images/background1.jpg").getImage());
		background.setSize(new Dimension(800, 600));
		this.setSize(800, 600);
		this.setLocation((toolkit.getScreenSize().width-800)/2, (toolkit.getScreenSize().height-600)/2);
		this.setContentPane(getFirstPanel());
		this.setTitle("AES System");
		getContentPane().add(background);

		pwLabel = new JLabel();
		pwLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		pwLabel.setBounds(207, 204, 140, 30);
		background.add(pwLabel);
		pwLabel.setText("SQL Password:");
		pwLabel.setForeground(Color.BLACK);
		
		userLabel = new JLabel();
		userLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		userLabel.setBounds(207, 169, 127, 30);
		background.add(userLabel);
		userLabel.setText("SQL User:");
		userLabel.setForeground(Color.BLACK);
		
		background.add(getUserTextField());
		background.add(getPWTextField());
		background.add(getConnectBtn());
		background.add(getCloseBtn());
		


	}
	/**
	 * gets the main panel
	 * 
	 * @return JPanel with the elements of the screen
	 */
	public JPanel getFirstPanel() {
		if (FirstPanel == null) {
			
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(252, 83, 297, 41));
			jLabel.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 40));
			jLabel.setText("AES Server");
			jLabel.setForeground(Color.blue);
			FirstPanel = new JPanel();
			FirstPanel.setLayout(null);
			//closeBtn.setVisible(false);
			FirstPanel.add(jLabel, null);

		}
		return FirstPanel; 
	}
	


	/**
	 * This method initializes closeBtn	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCloseBtn() {
		if (closeBtn == null) {
			closeBtn = new JButton();
			closeBtn.setBounds(292, 252, 200, 30);
			closeBtn.setText("Close Connection");
			closeBtn.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 18));
			closeBtn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					if (server != null)
					{
						server.stopListening();
						try {
							server.close();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						
						JOptionPane.showMessageDialog(null,"Server is shutting down");
						System.exit(ABORT);
					}
					else System.exit(ABORT);
				
				}
			});
		}
		return closeBtn;
	}
	
	
	/**
	 * This method initializes closeBtn	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getConnectBtn() {
		if (connectBtn == null) {
			connectBtn = new JButton();
			connectBtn.setBounds(533, 204, 140, 32);
			connectBtn.setText("Connect");
			connectBtn.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 18));
			connectBtn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("Connected to " + sqlUser + " with password " + sqlPW);
					jdbc = new JDBC();
					logEveryoneOut();
					userLabel.setVisible(false);
					pwLabel.setVisible(false);
					userTextField.setVisible(false);
					pwTextField.setVisible(false);
					connectBtn.setVisible(false);
					closeBtn.setVisible(true);
					server=new EchoServer(5555);
					 try 
					    {
						 server.listen(); //Start listening for connections
					    } 
					    catch (Exception ex) 
					    {
					      System.out.println("ERROR - Could not listen for clients!");
					    }
				}
			});
		}
		return connectBtn;
	}



	/**
	 * gets the clients connected to the server into a list model
	 * 
	 * @return DefaultListModel of clients connected to the server
	 */
	private DefaultListModel getConnectedClients()
	{
		clientConnectionModelList=new DefaultListModel();
		if(server.getNumberOfClients()==0)
		{
			clientConnectionModelList.addElement("No Connections at the moments");
		}
		else
		{
		Thread[] thrArr=null;
		clientConnectionModelList=new DefaultListModel();
		thrArr=server.getClientConnections();
		for(int i=0;i<server.getNumberOfClients();i++)
			clientConnectionModelList.addElement(thrArr[i]);
		}
			
		
		return clientConnectionModelList;
	}

	/**
	 * This method initializes lNameTextField	
	 */
	public JTextField getUserTextField() {
		if (userTextField == null) {
			userTextField = new JTextField();
			userTextField.setFont(new Font("Tahoma", Font.PLAIN, 18));
			userTextField.setBounds(352, 169, 140, 30);
			userTextField.setText("root");
		userTextField.addFocusListener(new java.awt.event.FocusAdapter() {
			public void focusLost(java.awt.event.FocusEvent e) {
				sqlUser=userTextField.getText();
			}
		});
		}
		return userTextField;
		
	}
	
	public JTextField getPWTextField() {
		if (pwTextField == null) {
			pwTextField = new JTextField();
			pwTextField.setFont(new Font("Tahoma", Font.PLAIN, 18));
			pwTextField.setBounds(352, 204, 140, 30);
			pwTextField.setText("root");
		pwTextField.addFocusListener(new java.awt.event.FocusAdapter() {
			public void focusLost(java.awt.event.FocusEvent e) {
				sqlPW=pwTextField.getText();
			}
		});
		}
		return pwTextField;
		
	}
	
	public void logEveryoneOut(){
		
		JDBC.updateStatmentCloumn("users", "userLogged","0");
	}
	
	public static void main(String[] args) {
		
		
		
		Server_GUI server = new Server_GUI();
		server.setVisible(true);
		server.setResizable(false);
		server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}

}  
